package com.zq.assistant.ui;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.zq.assistant.R;
import com.zq.assistant.func.apifactory.api.LifeCycle;
import com.zq.assistant.func.ioc.annotations.InjectListener;
import com.zq.assistant.func.ioc.listener.Listener;

/**
 * Created by zhangqiang on 17-5-10.
 */

public class BaseActivity extends AppCompatActivity implements LifeCycle{

    private ProgressDialog progressDialog;
    private AlertDialog alertDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @InjectListener(listener = Listener.OnCheckedChange,viewIds = R.id.checkBox)
    public void onButtonClick(CompoundButton compoundButton,boolean isChecked){
        Toast.makeText(BaseActivity.this, "我也成功了", Toast.LENGTH_SHORT).show();
    }

    protected static void initToolbar(final BaseActivity activity, Toolbar mToolbar){

        mToolbar.setTitle(activity.getResources().getString(R.string.app_name));
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onNavigationClick();
            }
        });
        activity.setSupportActionBar(mToolbar);
    }

    protected void onNavigationClick(){

        finish();
    }

    protected void showProgressDialog(CharSequence message){

        showProgressDialog("提示", message, true, false,null);
    }

    protected void showProgressDialog(CharSequence title,
                                      CharSequence message){

        showProgressDialog(title, message, true, false,null);
    }

    protected void showProgressDialog(CharSequence title,
                                      CharSequence message, boolean indeterminate){

        showProgressDialog(title, message, indeterminate, false,null);
    }

    protected void showProgressDialog(CharSequence message, boolean indeterminate){

        showProgressDialog("提示", message, indeterminate, false,null);
    }

    protected void showProgressDialog(CharSequence title,
                                      CharSequence message, boolean indeterminate,
                                      boolean cancelable){

        showProgressDialog(title, message, indeterminate, cancelable,null);
    }

    protected void showProgressDialog(CharSequence title,
                                      CharSequence message, boolean indeterminate,
                                      boolean cancelable, DialogInterface.OnCancelListener cancelListener){

//        progressDialog = new ProgressDialog(this);
//        progressDialog.setTitle(title);
//        progressDialog.setMessage(message);
//        progressDialog.setIndeterminate(false);
//        progressDialog.setCancelable(cancelable);
//        progressDialog.setOnCancelListener(cancelListener);
//        progressDialog.show();
        progressDialog = ProgressDialog.show(this,title,message,indeterminate,cancelable,cancelListener);
//        progressDialog.setMax(100);
//        progressDialog.setIndeterminate(false);
    }

    protected void hideProgressDialog(){

        if(progressDialog != null){
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    protected void updateDialogProgress(int progress,int max){
//&& !progressDialog.isIndeterminate()
        if(progressDialog != null ){
            progressDialog.setIndeterminate(false);
            progressDialog.setMax(max);
            progressDialog.setProgress(progress);
        }
    }


    protected void showAlertDialog(CharSequence title,
                                   CharSequence message,DialogInterface.OnClickListener onNeutralButtonClickListener){

        showAlertDialog(title, message, true, null, null, null, null, null, "确定", onNeutralButtonClickListener);
    }

    protected void showAlertDialog(CharSequence title,
                                   CharSequence message,
                                   boolean cancelable, DialogInterface.OnCancelListener cancelListener,
                                   String positiveButton, DialogInterface.OnClickListener onPositiveButtonClickListener){

        showAlertDialog(title, message, cancelable, cancelListener, positiveButton, onPositiveButtonClickListener, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        },null,null);
    }

    protected void showAlertDialog(CharSequence title,
                                   CharSequence message,
                                   boolean cancelable, DialogInterface.OnCancelListener cancelListener,
                                   String positiveButton, DialogInterface.OnClickListener onPositiveButtonClickListener,
                                   String negativeButton,DialogInterface.OnClickListener onNegativeButtonClickListener,
                                   String neutralButton,DialogInterface.OnClickListener onNeutralButtonClickListener){

        alertDialog = new AlertDialog.Builder(this)
                .setTitle(title)
                .setMessage(message)
                .setCancelable(cancelable)
                .setOnCancelListener(cancelListener)
                .setPositiveButton(positiveButton,onPositiveButtonClickListener)
                .setNegativeButton(negativeButton,onNegativeButtonClickListener)
                .setNeutralButton(neutralButton,onNeutralButtonClickListener)
                .create();
        alertDialog.show();
    }

    protected void showToast(String message){

        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    protected void showToast(int StringRes){
        showToast(getResources().getString(StringRes));
    }

    @Override
    protected void onStart() {
        super.onStart();
        onLifeStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        onLifeStop();
    }

    @Override
    public void onLifeStart() {

    }

    @Override
    public void onLifeStop() {

    }
}
